﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HackBoy_Implementation
{
    public enum PipeCommands : ushort
    {
        //Pipe Control
        CA_DisconnectPipe = 0x001, CA_AliveRequest, CA_IsAlive,

        //Error Control
        CA_Error = 0x050,

        //Commands
        CA_CommandsBegin = 0x100,
        CA_TestCommand, CA_SetSpeed, CA_MoveTo, CA_UseItem, CA_SelectTarget, CA_UseSkill,
        CA_LootItem, CA_SummonPet, CA_TalkToNpc, CA_QuestAccept, CA_QuestReward, CA_DestroyItem,
        CA_RepairItems, CA_ChangeRealm, CA_UseItemByItemId, CA_FollowPlayer, CA_Interact,
        CA_AutoLootOnOff,
        CA_CommandsEnd,

        //Requests
        CA_RequestsBegin = 0x300,
        CA_TestRequest, CA_GetCurrentTargetId, CA_PlayerIterFirst, CA_PlayerIterNext, CA_GetItemIdByIndex,
        CA_GetCoords, CA_GetDistFromPlayerToPlayer, CA_GetPlayerType, CA_GetPlayerName, CA_GetMyId,
        CA_GetHp, CA_GetPetPlayerId, CA_GetSelectedQuestId, CA_GetPetHp, CA_GetPlayerIdByName,
        CA_GetServerRealmInfo, CA_GetPartySize, CA_GetPartyMemberInfo, CA_GetClosestPlayerByName,
        CA_GetIsDead,
        CA_RequestsEnd
    }

    public enum MessageType : ushort
    {
        IS_NUMERIC = 0x00,
        IS_TEXT = 0x01,
        IS_COMMAND = 0x02,
        IS_REQUEST = 0x04,
        IS_RESPONSE = 0x08
    }

    class HackBoy
    {
        public PipeSystem PS;

        public HackBoy()
        { PS = new PipeSystem("Battle of Immortals-"); /* PS.sCharName = "CharName"; */ }

        public string PrintAgents()
        {
            string tempString = "";
            int myId = PS.CmdCB(PipeCommands.CA_GetMyId)[1].i;

            int iPlayer = 0;
            BaseMessage plIter = PS.CmdCB(PipeCommands.CA_PlayerIterFirst);
            do
            {
                iPlayer++;

                tempString += (String.Format("[{0:d}]\t{1} ({2:d}) HP={3}/{4}, Type={5}, Dist={6:0.00}\r\n",
                    iPlayer,
                    PS.CmdCB(PipeCommands.CA_GetPlayerName, plIter[2].i).buffer,
                    plIter[2].i,
                    PS.CmdCB(PipeCommands.CA_GetHp, plIter[2].i)[1].i,
                    PS.CmdCB(PipeCommands.CA_GetHp, plIter[2].i)[2].i,
                    PS.CmdCB(PipeCommands.CA_GetPlayerType, plIter[2].i)[1].i,
                    PS.CmdCB(PipeCommands.CA_GetDistFromPlayerToPlayer, myId, plIter[2].i)[1].f));

                plIter = PS.CmdCB(PipeCommands.CA_PlayerIterNext, plIter[1].i);
            } while (plIter[1].i != 0);

            return tempString;
        }

        public string GetPartyInfo()
        {
            string tempString = "";

            int partySize = PS.CmdCB(PipeCommands.CA_GetPartySize)[1].i;
            for (int i = 0; i < partySize; i++)
            {
                tempString += (String.Format("[{0:d}] {1} {2:d}/{3:d} Lvl{4:d} - Group Rank: {5:d} (realm: {6:d})\r\n",
                    i,
                    PS.CmdCB(PipeCommands.CA_GetPartyMemberInfo, i).buffer,
                    PS.InBuffer[1].i, PS.InBuffer[2].i,
                    PS.InBuffer[4].i, PS.InBuffer[3].i, PS.InBuffer[5].i));
            }

            return tempString;
        }

        public void RepairItems()
        { PS.Cmd(PipeCommands.CA_RepairItems);}

        public int GetQuestID()
        { return PS.CmdCB(PipeCommands.CA_GetSelectedQuestId)[1].i; }

        public int GetTargetID()
        { return PS.CmdCB(PipeCommands.CA_GetCurrentTargetId)[1].i; }

        public void LootItem()
        { PS.Cmd(PipeCommands.CA_LootItem); }

        public string GetCurrentTargetName()
        { return PS.CmdCB(PipeCommands.CA_GetPlayerName, GetTargetID()).buffer; }
    }
}
